package itsm.isperp.module.service.individuation;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.module.entity.individuation.IddProcessRelation;
import itsm.isperp.module.repository.individuation.IddProcessRelationMapper;
import itsm.isperp.workflow.service.BpmProcessDefinitaionService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2017-11-15 16:45
 */
@Service
public class IddProcessRelationService extends BaseService<IddProcessRelation> {

  @Autowired
  protected IddProcessRelationMapper iddProcessRelationMapper;

  @Override
  public IddProcessRelationMapper getRepository() {
    return this.iddProcessRelationMapper;
  }

  public Map<String,Object> getOutlineInfoAndCantRelateProcess(
      String defId,String nodeId) {
    Map<String,Object> result=new HashMap<String,Object>();
    BpmProcessDefinitaionService bpmProcessDefinitaionService = ContextHolder
        .getSpringBean("bpmProcessDefinitaionService");
    JdbcTemplate jdbcTemplate = ContextHolder
        .getSpringBean("jdbcTemplate");
    List<Map<String,String>> result1=new ArrayList<Map<String,String>>();
    ProcessDefinitionEntity proDefEntity = null;
    ActivityImpl curActivity = null;
    List<PvmTransition> outTrans = null;
    proDefEntity = bpmProcessDefinitaionService
        .getProcessDefinitionEntityById(defId);
    curActivity = proDefEntity.findActivity(nodeId);
    outTrans = curActivity.getOutgoingTransitions();
    for (PvmTransition tran : outTrans) {
      Map<String,String> m=new HashMap<String,String>();
      ActivityImpl act = (ActivityImpl) tran.getDestination();
      String actId = tran.getId();
      String nodeName = (String) tran.getProperty("name");
      m.put("nodeId", actId);
      m.put("nodeName", nodeName);
      result1.add(m);
    }
    String sql="SELECT REPLACE(NAME_,'.bpmn20.xml','') name_,key_ FROM act_re_model WHERE EXISTS(SELECT 1 FROM bpm_process_setting WHERE act_re_procdef_key=act_re_model.`KEY_` and use_for_relation=1) GROUP BY KEY_ ORDER BY last_update_time_";
    List<Map<String,Object>> result2=jdbcTemplate.queryForList(sql);
    result.put("outlineList", result1);
    result.put("relateProcessList", result2);
    return result;
  }
  
  public String getProcessNameByKey(String processKey)
  {
    String sql="SELECT REPLACE(NAME_,'.bpmn20.xml','') name_ FROM act_re_model where key_='"+processKey+"' ORDER BY last_update_time_ DESC LIMIT 1";
    JdbcTemplate jdbcTemplate = ContextHolder
        .getSpringBean("jdbcTemplate");
    String r=jdbcTemplate.queryForObject(sql, String.class);
    return r;
  }
  //1.OK;2:未发起关联的流程;3:发起关联流程未关闭
  public Integer getDealStatus(String key,String relateProcess)
  {
    JdbcTemplate jdbcTemplate = ContextHolder
        .getSpringBean("jdbcTemplate");
    String sql1="SELECT * FROM bpm_process_relation where from_id='"+key+"' and to_type='"+relateProcess+"'";
    List<Map<String,Object>>  list1=jdbcTemplate.queryForList(sql1);
    if(list1.size()==0)
    {
      return 2;
    }else
    {
      String sql2="SELECT * FROM db_entity WHERE id=(SELECT db_entity_id FROM bpm_process_setting WHERE act_re_procdef_key='"+relateProcess+"')";
      String id_=list1.get(0).get("to_id").toString();
      List<Map<String,Object>>  list2=jdbcTemplate.queryForList(sql2);
      if(list2.size()>0)
      {
        String tableName=list2.get(0).get("table_name").toString();
        String sql3="select status from "+tableName+" where  id='"+id_+"'";
        List<Map<String,Object>>  list3=jdbcTemplate.queryForList(sql3);
        if(list3.size()>0)
        {
          //ok
          String status=list3.get(0).get("status").toString();
          if(status.equals("已关闭"))
          {
            return 1;
          }else
          {
            return 3;
          }
         
        }else
        {
          return 2;
        }
      }
    }
    return 1;
  }
  
  

}
